#try/except来处理异常
# try:
#  x = int(input('Enter the first number: '))
#  y = int(input('Enter the second number: '))
#  print(x / y)
# except ZeroDivisionError:
#  print("The second number can't be zero!")

#进化成“计算器”
class MuffledCalculator:
 muffled = False#这里可以作为一个开关
 def calc(self, expr):#这里“输入表达式”
  try:
   return print(eval(expr))
  except ZeroDivisionError:
   if self.muffled:
    print('Division by zero is illegal')
   else:
    raise

#验证一下“计算器”
calculator = MuffledCalculator()
calculator.calc('10 / 2')
# calculator.calc('10 / 0')    如果没有25行的代码，这里将会报错
calculator.muffled = True      #所以这里是打开开关
calculator.calc('10 / 0')

#多个except子句，处理多种异常
try:
 x = int(input('Enter the first number: '))
 y = int(input('Enter the second number: '))
 print(x / y)
except ZeroDivisionError:
 print("The second number can't be zero!")
except TypeError:
 print("That wasn't a number, was it?")
#如果要使用一个except子句捕获多种异常，可在一个元组中指定这些异常
try:
 x = int(input('Enter the first number: '))
 y = int(input('Enter the second number: '))
 print(x / y)
except (ZeroDivisionError, TypeError, NameError):
 print('Your numbers were bogus ...')
#如果想把具体的错误打印下来，可以将最后两行代码替换成
# except (ZeroDivisionError, TypeError) as e:
#  print(e)

#如果想在非法输入时让程序提供给用户重新输入的入口，应该怎么实现
while True:
 try:
  x = int(input('Enter the first number: '))
  y = int(input('Enter the second number: '))
  value = x / y
  print('x / y is', value)
 except Exception as e:
  print('Invalid input:', e)
  print('Please try again')
 else:
  break#如果没有引发异常则